package com.ww.springboot.boot.controller;

import lombok.AllArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-02-21 15:28
 */
@RestController
@RequestMapping("lua")
@AllArgsConstructor
public class RedisLuaController {


    private final StringRedisTemplate redisTemplate;


    @RequestMapping("/test1")
    public String secKill() {

        long ttlMillis = TimeUnit.MINUTES.toMillis(1);
        String redisKeyBuilder = "aa";
        List<String> keys = Collections.singletonList(redisKeyBuilder);
        DefaultRedisScript redisScript = new DefaultRedisScript();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("META-INF/scripts/blade_rate_limiter.lua")));
        redisScript.setResultType(List.class);

        long max = 100L;
        List<Long> results = (List) this.redisTemplate.execute(redisScript, keys, new Object[]{max + "", ttlMillis + "", System.nanoTime() + ""});
        System.out.println(results);
        return "";
    }
}
